import { loadSprite } from '../Loader/index'

const backToHome = () => {
  // TODO: 确认后离开
  cc.director.loadScene('home')
}
/** 
 * @author 刘建文
 * @createAt 2019-12-21 17:26:50
 * @description 返回主页
 * @param _this 需要执行的实例
 * @todo 添加参数，以支持特定返回逻辑
 */
export const setHomeBtn = async (_this) => {
  const node = new cc.Node('home_btn')
  _this.node.addChild(node)
  node.addComponent(cc.Button)
  node.on(cc.Node.EventType.TOUCH_START, backToHome.bind(_this))

  const backNode = new cc.Node('home_btn_background')
  node.addChild(backNode)
  const res = await loadSprite('image/home_btn', cc.SpriteFrame)
  backNode.addComponent(cc.Sprite).spriteFrame = res
  node.width = node.height = backNode.width = backNode.height = 50
  // 定位到全局左上角
  const widget = node.addComponent(cc.Widget)
  widget.target = _this.node
  widget.isAlignLeft = true
  widget.isAlignTop = true
  widget.left = 10
  widget.top = 10
  node.position = _this.node.convertToNodeSpaceAR(cc.v2(0,_this.node.height))
  
}
export default { setHomeBtn }
